/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import doa.LoginDAO;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import models.Admin;
import models.Twitterer;
import models.User;
import utils.JSFSessionHandler;

/**
 *
 * @author Misja
 */
public class LoginBean implements Serializable {

    private User user;

    public LoginBean() {
        this.user = new User();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String login() {
        Twitterer tempTwitterer = LoginDAO.getTwittererByNamePassword(this.user.getUserName(), this.user.getPassword());
        if (tempTwitterer != null) {
            JSFSessionHandler.putObjectInSession("currentUser", tempTwitterer);
            return "/Home.xhtml?faces-redirect=true";
        }
        
        Admin tempAdmin = LoginDAO.getAdminByNamePassword(this.user.getUserName(), this.user.getPassword());
        if (tempAdmin != null) {
            JSFSessionHandler.putObjectInSession("currentUser", tempTwitterer);
            return "/AuthorizationOverview.xhtml?faces-redirect=true"; // naar admin pagina
        }
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Username or password incorrect", ""));
        return null;
    }
    
     public String toNewTwitterer(){
        return "/NewTwitterer.xhtml?faces-redirect=true";
    }
     
    public String logout(){
        JSFSessionHandler.clearSession();
        return "/Login.xhtml?faces-redirect=true";
    };
    
}
 
